package com.yxhl.zoume.utils.timepicker;

import android.content.Context;
import android.support.annotation.NonNull;

import com.bigkoo.pickerview.OptionsPickerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by vhawk on 15/11/7
 * Modify by sugarya on 16/08/12
 * 时间选择器,数据生成与处理类
 */
public class TimePickerHelper {

    private OptionsPickerView<String> mOptionsPickerView;

    // 定制巴士相关
    private ArrayList<String> mZouMeBusOptions1Items;
    private ArrayList<ArrayList<String>> mZouMeBusOptions2Items;

    //快车相关
    private ArrayList<String> mSpecialCarOptions1DayList;
    private ArrayList<ArrayList<String>> mSpecialCarOptions2TimeList;

    //公务车相关


    private TimePickerHelper(Context context) {
        mOptionsPickerView = new OptionsPickerView<>(context);
    }

    public static TimePickerHelper newInstance(Context context) {

        return new TimePickerHelper(context);
    }


    /**
     * 快车 出行时间选择器
     *
     * @param specialCarPickerSelectListener
     */
    public void showSpecialCarPicker(SpecialCarPickerSelectListener specialCarPickerSelectListener) {
        getSpecialCarPicker().show();
        renderSpecialCarPickerResult(specialCarPickerSelectListener);
    }

    /**
     * 定制巴士, 时间选择
     * by Xboy
     */
    public void showZouMeBusTimePicker(String dateSelected, OnTimeOptionSelectListener onTimeOptionSelectListener) {
        getZouMeBusTimerPicker(dateSelected).show();
        renderZouMeBusTime(onTimeOptionSelectListener);
    }


    protected OptionsPickerView<String> getSpecialCarPicker() {

        mSpecialCarOptions1DayList = getSpecialCarOptions1DayList();
        mSpecialCarOptions2TimeList = getSpecialCarOptions2TimeList();

        mOptionsPickerView.setPicker(mSpecialCarOptions1DayList, mSpecialCarOptions2TimeList, true);

        mOptionsPickerView.setCyclic(false, true, false);

        return mOptionsPickerView;
    }

    /**
     * 定制巴士-时间选择器
     * 格式: 6月3日 13:30
     *
     * @return
     */
    private OptionsPickerView<String> getZouMeBusTimerPicker(String dateSelected) {

        mZouMeBusOptions2Items = new ArrayList<>();

        //默认选中当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());

        int currentHours = calendar.get(Calendar.HOUR_OF_DAY);
        int currentMinutes = calendar.get(Calendar.MINUTE);


        // using
        ArrayList<String> allHours = new ArrayList<>();


        /**点:分数据 - options3*/
        /**!!!!!!!!定制巴士需要提前至少2小时!!!!!!!!!*/

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String today = format.format(new Date(System.currentTimeMillis()));

        if (today.equals(dateSelected)) {// 今天
            if (currentMinutes < 50) {
                // 第一个可选'时'
                String firshHour = String.valueOf(currentHours + 2) + " 点";
                allHours.add(firshHour);
                ArrayList<String> allMinutes = new ArrayList<>();

                // 第一个可选'时'的第一个可选'分'
                int firstMin;
                if (currentMinutes % 10 == 0 || currentMinutes % 10 >= 5) {
                    firstMin = ((currentMinutes / 10 + 1) * 10 + 5);
                    String firstMinStr;
                    if (60 > firstMin) {
                        firstMinStr = String.valueOf(firstMin);
                    } else if (60 == firstMin) {
                        firstMinStr = "00";
                    } else {
                        firstMinStr = "05";
                    }

                    String first = firstMinStr + " 分";
                    allMinutes.add(first);
                } else {
                    firstMin = (currentMinutes / 10 + 1) * 10;
                    String first = String.valueOf(firstMin) + " 分";
                    allMinutes.add(first);
                }

                // 第一个可选'时'内其他可选'分'
                for (int m = firstMin + 5; m <= 55; m += 5) {
                    String hm = String.valueOf(m) + " 分";
                    allMinutes.add(hm);
                }
                mZouMeBusOptions2Items.add(allMinutes);
            }

            for (int h = currentHours + 3; h < 24; h++) {
                allHours.add(String.valueOf(h) + " 点");
                ArrayList<String> allMinutes = new ArrayList<>();

                for (int m = 0; m <= 55; m += 5) {
                    String hm;
                    if (m < 10) {
                        hm = "0" + m;
                    } else {
                        hm = String.valueOf(m);
                    }
                    allMinutes.add(hm + " 分");
                }

                mZouMeBusOptions2Items.add(allMinutes);
            }
        } else {// 明天、后天.....
            for (int h = 0; h < 24; h++) {
                allHours.add(String.valueOf(h) + " 点");
                ArrayList<String> allMinutes = new ArrayList<>();

                for (int m = 0; m <= 55; m += 5) {
                    String hm;
                    if (m < 10) {
                        hm = "0" + m;
                    } else {
                        hm = String.valueOf(m);
                    }
                    allMinutes.add(hm + " 分");
                }

                mZouMeBusOptions2Items.add(allMinutes);
            }
        }
        mZouMeBusOptions1Items = new ArrayList<>();
        mZouMeBusOptions1Items.addAll(allHours);
        mOptionsPickerView.setPicker(mZouMeBusOptions1Items, mZouMeBusOptions2Items, true);
        mOptionsPickerView.setCyclic(false, false, false);

        return mOptionsPickerView;
    }


    public OptionsPickerView<String> getOptionsPickerView() {
        return mOptionsPickerView;
    }


    private void renderSpecialCarPickerResult(final SpecialCarPickerSelectListener specialCarPickerSelectListener) {
        if (mOptionsPickerView != null) {
            mOptionsPickerView.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int option2, int options3) {

                    String optionResult1 = mSpecialCarOptions1DayList.get(options1) + " " + mSpecialCarOptions2TimeList.get(options1).get(option2);
                    String optionResult2 = getSpecialCarOutputDateTime(options1, option2);

                    specialCarPickerSelectListener.onConfirmPick(optionResult1, optionResult2);
                }
            });
        }
    }

    /**
     * 定制巴士,出行时间
     *
     * @param onTimeOptionSelectListener
     */
    private void renderZouMeBusTime(final OnTimeOptionSelectListener onTimeOptionSelectListener) {
        if (null != mOptionsPickerView) {
            mOptionsPickerView.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int option2, int options3) {
                    // 13点  40分
                    String hourSelected = mZouMeBusOptions1Items.get(options1).replace(" 点", "");
                    String minuteSelected = mZouMeBusOptions2Items.get(options1).get(option2).replace(" 分", "");
                    onTimeOptionSelectListener.onTimeOptionSelect(hourSelected + ":" + minuteSelected);
                }
            });
        }
    }

    /**
     * 获取快车时间选择器选项1数据
     *
     * @return
     */
    private ArrayList<String> getSpecialCarOptions1DayList() {
        ArrayList<String> dataList = new ArrayList<>();
        dataList.add("今天");
        dataList.add("明天");
        dataList.add("后天");
        return dataList;
    }

    /**
     * 获取快车时间选择器选项2数据
     *
     * @return
     */
    private ArrayList<ArrayList<String>> getSpecialCarOptions2TimeList() {
        ArrayList<ArrayList<String>> timeList = new ArrayList<>();

        //今天对应的时刻点
        ArrayList<String> timeListToday = getSpecialCarTodayByCurrentTime();
        timeList.add(timeListToday);

        //明天/后天对应的时刻点
        ArrayList<String> mWholeDay = getSpecialCarWholeDay();
        timeList.add(mWholeDay);
        timeList.add(mWholeDay);

        return timeList;
    }

    /**
     * 今天当下,对应的时分段
     *
     * @return
     */
    @NonNull
    private ArrayList<String> getSpecialCarTodayByCurrentTime() {

        ArrayList<String> timeListToday = new ArrayList<>();
        timeListToday.add("现在");

        List<Integer> currentTime = getSpecialCarCurrentHourAndMinute();
        int currentHour = currentTime.get(0);
        int currentMinute = currentTime.get(1);

        for (int i = currentHour; i < 24; i++) {
            if (i == currentHour) {
                for (int j = currentMinute / 10 + 1; j < 6; j++) {
                    timeListToday.add(currentHour + ":" + j + "0");
                }
            } else {
                for (int j = 0; j < 6; j++) {
                    timeListToday.add(i + ":" + j + "0");
                }
            }
        }

        return timeListToday;
    }

    /**
     * 获取当前时间的小时数和分钟数
     *
     * @return
     */
    private List<Integer> getSpecialCarCurrentHourAndMinute() {

        List<Integer> hourAndMinList = new ArrayList<>();

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String timeStr = formatter.format(curDate);

        String currentHour = (String) timeStr.subSequence(timeStr.indexOf(" ") + 1, timeStr.indexOf(":"));
        String currentMin = (String) timeStr.subSequence(timeStr.indexOf(":") + 1, timeStr.indexOf(":") + 3);

        hourAndMinList.add(Integer.parseInt(currentHour));
        hourAndMinList.add(Integer.parseInt(currentMin));

        return hourAndMinList;
    }

    /**
     * 明天/后天,一个完整的全天的时刻段
     *
     * @return
     */
    @NonNull
    private ArrayList<String> getSpecialCarWholeDay() {
        ArrayList<String> timeListTomorrow = new ArrayList<>();

        for (int i = 0; i < 24; i++) {

            for (int j = 0; j < 6; j++) {
                String timeTemp = i + ":" + j + "0";
                timeListTomorrow.add(timeTemp);
            }
        }

        return timeListTomorrow;
    }

    /**
     * 用户选中option后,输出所需的时间日期格式
     *
     * @param dayIndex
     * @param hourIndex
     * @return
     */
    private String getSpecialCarOutputDateTime(int dayIndex, int hourIndex) {

        String timeStr = "";

        if (dayIndex == 0 && hourIndex == 0) {//今天，现在，单独处理
            hourIndex = 1;
        }

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间
        Date curDate = new Date(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * dayIndex);
        String timeStrTemp = formatter.format(curDate);
        // 获取年月日
        timeStr += timeStrTemp.substring(0, timeStrTemp.indexOf(" "));
        // 加分隔符
        timeStr += " ";
        //获取小时和分钟
        timeStr += getSpecialCarOptions2TimeList().get(dayIndex).get(hourIndex);

        return timeStr;
    }

    public interface SpecialCarPickerSelectListener {

        /**
         * @param optionResult1 返回结果,格式如下"dd HH:mm"
         * @param optionResult2 返回结果,格式如下"yyyy-MM-dd HH:mm"
         */
        void onConfirmPick(String optionResult1, String optionResult2);
    }

    public interface OnTimeOptionSelectListener {
        /**
         * 返回选中的时间
         *
         * @param timeSelected
         */
        void onTimeOptionSelect(String timeSelected);
    }
}


